﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using YXShop.Model.Order;

namespace YXShop.Web.filehandle
{
    public partial class getorders : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.OrderAction();
            }
            else
            {
                this.GetMessage();
            }
        }
        private void GetMessage()
        {
            Response.Write("非法提交数据！");
            Response.Redirect(Request.UrlReferrer.ToString());
        }

        private void OrderAction()
        {
            string username = "";
            if (System.Web.HttpContext.Current.Session["MemberName"] != null)
            {
                username = System.Web.HttpContext.Current.Session["MemberName"].ToString();
            }
            ProfileCommon Profile = new ProfileCommon();
            ICollection<CartItemInfo> cart = Profile.ShoppingCart.CartItems;
            if (cart.Count > 0)
            {
                BLL.Member.MemberInfo user = new YXShop.BLL.Member.MemberInfo();
                BLL.Order.Orders bll = new YXShop.BLL.Order.Orders();
                Model.Order.Orders model = new YXShop.Model.Order.Orders();
                string[] strPayment = ChangeHope.WebPage.PageRequest.GetFormString("paymentMethod").Split('|');
                int paymentId = Convert.ToInt32(strPayment[0]);
                decimal paymentFee = Convert.ToDecimal(strPayment[1]);
                model.UserId = username;
                model.WhetherCouAndinte = "0";
                model.ConsigneeName = ChangeHope.WebPage.PageRequest.GetFormString("userName");
                model.ConsigneeRealName = ChangeHope.WebPage.PageRequest.GetFormString("ConstructionSigns");
                model.ConsigneeAddress = ChangeHope.WebPage.PageRequest.GetFormString("userAddr");
                model.ConsigneeTel = ChangeHope.WebPage.PageRequest.GetFormString("userPhone");
                model.ConsigneeFax = ChangeHope.WebPage.PageRequest.GetFormString("ConsigneTime");
                model.ConsigneeZip = ChangeHope.WebPage.PageRequest.GetFormString("userZip");
                model.ConsigneeProvince = ChangeHope.WebPage.PageRequest.GetFormString("ConsigneTime");
                model.ConsigneePhone = ChangeHope.WebPage.PageRequest.GetFormString("userMobile");
                model.ConsigneeEmail = ChangeHope.WebPage.PageRequest.GetFormString("userEmail");
                model.Carriage = Convert.ToDecimal(ChangeHope.WebPage.PageRequest.GetFormString("DeliveryMode"));
                model.PaymentType = paymentId.ToString();
                model.Payment = 0;
                model.FactPrice = 0;
                model.Invoice = 0;
                model.OrderDate = System.DateTime.Now;
                model.OrderId = "";
                model.OrderStatus = 0;
                model.ParvalueAndInte = 0;

                model.ShopDate = System.DateTime.Now;
                model.TotalPrice = 0;
                model.OrderType = 0;
                model.SaleUserID = ChangeHope.WebPage.PageRequest.GetFormString("putoutId");
                model.SaleUserType = ChangeHope.WebPage.PageRequest.GetFormString("putoutTypeId");
                //下面是判断各种情况的出现
                //第一种情况，判断订单的接收方
                YXShop.Common.SysParameter sys = new YXShop.Common.SysParameter();
                bool ispaymentweboperation = sys.IsPayMentWebOperation;
                //网站运营商 1
                //单独的商家 0
                double allprice = 0.0;
                decimal weight = 0;
                int productCount = 0;
                int courierId = Convert.ToInt32(ChangeHope.WebPage.PageRequest.GetFormString("DeliveryMode"));
                //读取该人员所有的购物车。
                BLL.Product.ProductInfo bllProductInfo = new YXShop.BLL.Product.ProductInfo();

                YXShop.TemplateAction.Common common = new YXShop.TemplateAction.Common();

                BLL.Order.OrderProduct bllOrderProduct = new YXShop.BLL.Order.OrderProduct();
                Model.Order.OrderProduct modelOrderProduct = new YXShop.Model.Order.OrderProduct();
                YXShop.BLL.Product.ProductSpecification spebll = new YXShop.BLL.Product.ProductSpecification();
                YXShop.BLL.Product.ProductSparepart spabll = new YXShop.BLL.Product.ProductSparepart();
                string StrOrders = sys.OrderId;
                YXShop.BLL.Order.ShoppingCard shopCartbll = new YXShop.BLL.Order.ShoppingCard();
                if (ispaymentweboperation)
                {
                    model.SaleUserID = "0";
                    model.SaleUserType = "0";
                    model.Courier = 0;
                    model.BusinessmanID = "0";
                    model.ReceiverId = "0";
                    model.OrderId = StrOrders;
                    model.TotalPrice = 0;
                    model.Remark = "";
                    model.PaymentStatus = 0;
                    int id = bll.Add(model);
                    foreach (CartItemInfo info in cart)
                    {
                        Model.Product.ProductInfo modelProductInfo = bllProductInfo.GetModel(Convert.ToInt32(info.ProductId));
                        if (modelProductInfo != null)
                        {
                            double price = double.Parse(modelProductInfo.pro_ShopPrice.ToString());
                            //配件
                            string fittingProductInfo = "";
                            int fittingProductCount = 0;
                            double fittingProductWeight = 0;
                            double fittingProductIntergal = 0;
                            double fittingTotalPrice = 0;
                            YXShop.Common.ProductInfo.FittingDisposal(info.SparepartId, info.FittingsProductId, info.FittingsProductCount, out fittingProductCount, out fittingProductInfo, out fittingTotalPrice, out fittingProductWeight, out fittingProductIntergal);
                            //规格
                            string memberPrice = modelProductInfo.pro_RatingDiscount;
                            string productNo = modelProductInfo.pro_NO;

                            if (!string.IsNullOrEmpty(modelProductInfo.pro_Specifications.Trim()))
                            {
                                YXShop.Common.ProductInfo.SpecificationDisposal(Convert.ToInt32(info.ProductId), info.Specification, memberPrice, price, out price, out productNo);
                            }

                            modelOrderProduct.AddTime = System.DateTime.Now;
                            modelOrderProduct.OrderId = id;
                            modelOrderProduct.ProId = Convert.ToInt32(info.ProductId);
                            modelOrderProduct.ProClass = modelProductInfo.pro_CID;
                            modelOrderProduct.ProImg = modelProductInfo.pro_Thumbnail;
                            modelOrderProduct.ProName = modelProductInfo.pro_Name;
                            weight += ((decimal)modelProductInfo.pro_Weight) + Convert.ToDecimal(fittingProductWeight);
                            modelOrderProduct.ProNum = Decimal.Parse(info.Quantity.ToString());
                            modelOrderProduct.ProOtherPara = "";
                            modelOrderProduct.ProPrice = Convert.ToDecimal(price);
                            modelOrderProduct.FittingsProductPrice = fittingTotalPrice.ToString();
                            modelOrderProduct.FittingsProductId = info.FittingsProductId;
                            modelOrderProduct.FittingsProductCount = info.FittingsProductCount;
                            modelOrderProduct.Specification = info.Specification;
                            modelOrderProduct.FittingsId = !string.IsNullOrEmpty(info.SparepartId.Trim()) ? Convert.ToInt32(info.SparepartId) : 0;
                            productCount += Convert.ToInt32(info.Quantity) + fittingProductCount;
                            allprice += (Convert.ToDouble(price.ToString()) * (double)info.Quantity) + fittingTotalPrice;
                            bllOrderProduct.Add(modelOrderProduct);
                        }
                        shopCartbll.DeleteCartkey(info.CartKey);
                    }
                    //计算运费
                    string delivery = YXShop.Common.OrderInfo.TotalFreight(courierId, float.Parse(weight.ToString()), float.Parse(allprice.ToString()), productCount);
                    model.Courier = Convert.ToDecimal(delivery);
                    model.TotalPrice = (decimal)allprice;
                    model.FactPrice = (decimal)allprice + Convert.ToDecimal(delivery) + paymentFee;
                    model.Id = id;
                    bll.Update(model);
                    Profile.ShoppingCart.Clear();
                }
                else
                {
                    string BusinessmanID = "0";
                    if (ChangeHope.WebPage.PageRequest.GetFormString("putoutId") != "")
                    {
                        YXShop.BLL.Shop.Shop shopBll = new YXShop.BLL.Shop.Shop();
                        List<YXShop.Model.Shop.Shop> shopList = shopBll.GetListShopByWhere(" uid=" + ChangeHope.WebPage.PageRequest.GetFormString("putoutId"));
                        if (shopList.Count > 0)
                        {
                            BusinessmanID = shopList[0].Sid.ToString();
                        }
                    }
                    model.SaleUserID = ChangeHope.WebPage.PageRequest.GetFormString("putoutId");
                    model.SaleUserType = ChangeHope.WebPage.PageRequest.GetFormString("putoutTypeId");
                    model.Courier = 0;
                    model.BusinessmanID = BusinessmanID;
                    model.ReceiverId = "0";
                    model.OrderId = StrOrders;
                    model.TotalPrice = 0;
                    model.Remark = "";
                    model.PaymentStatus = 0;
                    int id = bll.Add(model);

                    foreach (CartItemInfo info in cart)
                    {
                        Model.Product.ProductInfo modelProductInfo = bllProductInfo.GetModel(Convert.ToInt32(info.ProductId));
                        if (modelProductInfo != null)
                        {
                            double price = double.Parse(modelProductInfo.pro_ShopPrice.ToString());
                            //配件
                            string fittingProductInfo = "";
                            int fittingProductCount = 0;
                            double fittingProductWeight = 0;
                            double fittingProductIntergal = 0;
                            double fittingTotalPrice = 0;
                            YXShop.Common.ProductInfo.FittingDisposal(info.SparepartId, info.FittingsProductId, info.FittingsProductCount, out fittingProductCount, out fittingProductInfo, out fittingTotalPrice, out fittingProductWeight, out fittingProductIntergal);
                            //规格
                            string memberPrice = modelProductInfo.pro_RatingDiscount;
                            string productNo = modelProductInfo.pro_NO;

                            if (!string.IsNullOrEmpty(modelProductInfo.pro_Specifications.Trim()))
                            {
                                YXShop.Common.ProductInfo.SpecificationDisposal(Convert.ToInt32(info.ProductId), info.Specification, memberPrice, price, out price, out productNo);
                            }
                            modelOrderProduct.AddTime = System.DateTime.Now;
                            modelOrderProduct.OrderId = id;
                            modelOrderProduct.ProId = Convert.ToInt32(info.ProductId);
                            modelOrderProduct.ProClass = modelProductInfo.pro_CID;
                            modelOrderProduct.ProImg = modelProductInfo.pro_Thumbnail;
                            modelOrderProduct.ProName = modelProductInfo.pro_Name;
                            weight += ((decimal)modelProductInfo.pro_Weight) + Convert.ToDecimal(fittingProductWeight);
                            modelOrderProduct.ProNum = Decimal.Parse(info.Quantity.ToString());
                            modelOrderProduct.ProOtherPara = "";
                            modelOrderProduct.ProPrice = Convert.ToDecimal(price); ;
                            modelOrderProduct.FittingsProductPrice = fittingTotalPrice.ToString();
                            modelOrderProduct.FittingsProductId = info.FittingsProductId;
                            modelOrderProduct.FittingsProductCount = info.FittingsProductCount;
                            modelOrderProduct.Specification = info.Specification;
                            modelOrderProduct.FittingsId = !string.IsNullOrEmpty(info.SparepartId.Trim()) ? Convert.ToInt32(info.SparepartId) : 0;
                            productCount += Convert.ToInt32(info.Quantity) + fittingProductCount;
                            allprice += (Convert.ToDouble(price.ToString()) * (double)info.Quantity) + fittingTotalPrice;
                            bllOrderProduct.Add(modelOrderProduct);
                        }
                        shopCartbll.DeleteCartkey(info.CartKey);

                    }

                    //计算运费
                    string delivery = YXShop.Common.OrderInfo.TotalFreight(courierId, float.Parse(weight.ToString()), float.Parse(allprice.ToString()), productCount);
                    model.Courier = Convert.ToDecimal(delivery);
                    model.TotalPrice = (decimal)allprice;
                    model.FactPrice = (decimal)allprice + Convert.ToDecimal(delivery) + paymentFee;
                    model.Id = id;
                    bll.Update(model);

                }
                Profile.ShoppingCart.Clear();
                string ReturnUrl = YXShop.TemplateAction.Common.UrlStr("paymentorders", "OrderId=" + StrOrders + "", "product/");
                HttpContext.Current.Response.Redirect(ReturnUrl);
            }
            else
            {
                Response.Write("对不起，您还没有购买任何商品");
            }
        }
    }
}
